package com.mes.basicdata.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.dao.iface.IPartDao;
import com.mes.basicdata.domain.Part;
import com.mes.common.dao.BasicDao;

@Repository
public class PartDao extends BasicDao<Part> implements IPartDao {

	@Override
	public void getPartByConditions(Page<Part> page, Map<String, Object> params) throws Exception {
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from " + Part.class.getName() + " a where 1=1 ");

		String drawingId = (String) params.get("drawingId");
		if (StringUtils.isNotEmpty(drawingId)) {
			coreHql.append(" and a.drawingId like :drawingId ");
			args.put("drawingId", "%" + drawingId + "%");
		}

		String partName = (String) params.get("partName");
		if (StringUtils.isNotEmpty(partName)) {
			coreHql.append(" and a.partName like :partName ");
			args.put("partName", "%" + partName + "%");
		}

		String description = (String) params.get("description");
		if (StringUtils.isNotEmpty(description)) {
			coreHql.append(" and description like :description ");
			args.put("description", "%" + description + "%");
		}

		String partType = (String) params.get("partType");
		if (StringUtils.isNotEmpty(partType)) {
			coreHql.append(" and a.partType = :partType ");
			args.put("partType", partType);
		}

		String productDeptUid = (String) params.get("productDeptUid");
		if (StringUtils.isNotEmpty(productDeptUid)) {
			coreHql.append(" and a.productDeptUid = :productDeptUid ");
			args.put("productDeptUid", productDeptUid);
		}

		String countHql = "select count(*) " + coreHql.toString();
		// String hql = coreHql.toString() + "order by a.dept.id,a.id";
		String hql = coreHql.toString();

		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while (iterator.hasNext()) {
			key = (String) iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":" + key, ":" + newKey);
			countHql = countHql.replace(":" + key, ":" + newKey);
		}
		this.pagingQuery(page, hql, countHql, newArgs);
	}

	/**
	 * 通过条件查询part
	 * @param drawingId
	 * @param partName
	 * @param partType
	 * @param productDeptId
	 * @return
	 */
	public List<Part> getPartByInformation(String drawingId, String partName, String partType, String productDeptUid,Page<Part> page) {
		List<Part> list = new ArrayList<>();
		
		DetachedCriteria crit = DetachedCriteria.forClass(Part.class);
		if(!drawingId.equals("*")) 
			crit.add(Restrictions.like("drawingId", "%" + drawingId + "%"));
		if(!partName.equals("*")) 
			crit.add(Restrictions.like("partName", "%" + partName + "%"));
		if(!partType.equals("*")) 
			crit.add(Restrictions.like("partType", "%" + partType + "%"));
		if(!productDeptUid.equals("*")) 
			crit.add(Restrictions.like("productDeptUid", "%" + productDeptUid + "%"));
		if (page != null) {
			this.pagingQuery(page, crit);
		} else {
			list = this.query(crit, null);
		}
		return list;
	}
}
